package com.ph.irp.base.materialassistantattribute.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.ph.irp.base.materialassistantattribute.dao.AssistantAttributeTypeDao;
import com.ph.irp.model.PBaseMaterialAssistantattributetype;
import com.ph.util.DBUtil;

/**
 * The Class AssistantAttributeTypeDaoImpl.
 * 
 * @author SongYandong song.yandong@phinfoservices.com
 * @version 1.0 <br>
 *          Copyright (C), 2009, SongYandong <br>
 *          This program is protected by copyright laws. <br>
 *          Program Name: AssistantAttributeTypeDaoImpl.java<br>
 *          Date:2009-11-16
 */
public class AssistantAttributeTypeDaoImpl implements AssistantAttributeTypeDao {

	private ComboPooledDataSource dataSource;

	public ComboPooledDataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(ComboPooledDataSource dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * 添加物料辅助属性类别方法
	 */
	@Override
	public void addAttributeType(PBaseMaterialAssistantattributetype model)
	throws Throwable {
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			pStmt = con
					.prepareStatement("insert into P_base_material_assistantattributetype(AssistantAttributeTypeCode,AssistantAttributeTypeName,AttributeBasicType) values (?,?,?)");
			pStmt.setString(1, model.getAssistantAttributeTypeCode());
			pStmt.setString(2, model.getAssistantAttributeTypeName());
			pStmt.setInt(3, model.getAttributeBasicType());
			pStmt.executeUpdate();
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			DBUtil.setNullObject(con, pStmt, rs);
		}
	}

	/**
	 * 删除物料辅助属性方法
	 */
	@Override
	public void deleteAttributeType(int id) throws Throwable {
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			pStmt = con
					.prepareStatement("delete from P_base_material_assistantattributetype where AssistantAttributeTypeID = ?");
			pStmt.setInt(1, id);
			pStmt.executeUpdate();
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			DBUtil.setNullObject(con, pStmt, rs);
		}

	}

	/**
	 * 条件查询
	 * 
	 */
	@Override
	public List<PBaseMaterialAssistantattributetype> getAllAttributeType(
			PBaseMaterialAssistantattributetype model) throws Throwable {
		List<PBaseMaterialAssistantattributetype> list = new ArrayList<PBaseMaterialAssistantattributetype>();
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		StringBuilder sb = new StringBuilder();
		sb
				.append("select * from P_base_material_assistantattributetype where 1=1");
		if (model.getAssistantAttributeTypeCode() != null
				&& !model.getAssistantAttributeTypeCode().equals("")) {
			sb.append(" and AssistantAttributeTypeCode like '%");
			sb.append(model.getAssistantAttributeTypeCode());
			sb.append("%'");
		}
		if (model.getAssistantAttributeTypeName() != null
				&& !model.getAssistantAttributeTypeName().equals("")) {
			sb.append(" and AssistantAttributeTypeName like '%");
			sb.append(model.getAssistantAttributeTypeName());
			sb.append("%'");
		}
		 if (String.valueOf(model.getAttributeBasicType()) != null
		 && !String.valueOf(model.getAttributeBasicType()).equals("")) {
		 sb.append(" and AttributeBasicType =");
		 sb.append(model.getAttributeBasicType());
		 }
		try {
			con = dataSource.getConnection();
			System.out.println(sb.toString());
			pStmt = con.prepareStatement(sb.toString());
			rs = pStmt.executeQuery();

			while (rs.next()) {
				PBaseMaterialAssistantattributetype pma = new PBaseMaterialAssistantattributetype();
				pma.setAssistantAttributeTypeId(rs
						.getInt("AssistantAttributeTypeID"));
				pma.setAssistantAttributeTypeCode(rs
						.getString("AssistantAttributeTypeCode"));
				pma.setAssistantAttributeTypeName(rs
						.getString("AssistantAttributeTypeName"));
				pma.setAttributeBasicType(rs.getInt("AttributeBasicType"));

				list.add(pma);
			}
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			DBUtil.setNullObject(con, pStmt, rs);
		}
		return list;
	}

	/**
	 * 根据ID查询
	 */
	@Override
	public PBaseMaterialAssistantattributetype getAttrobuteTypeById(int id)
	throws Throwable {
		PBaseMaterialAssistantattributetype model = new PBaseMaterialAssistantattributetype();
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			pStmt = con
					.prepareStatement("select * from P_base_material_assistantattributetype where AssistantAttributeTypeID = ?");
			pStmt.setInt(1, id);
			rs = pStmt.executeQuery();

			while (rs.next()) {
				model.setAssistantAttributeTypeId(rs
						.getInt("AssistantAttributeTypeID"));
				model.setAssistantAttributeTypeCode(rs
						.getString("AssistantAttributeTypeCode"));
				model.setAssistantAttributeTypeName(rs
						.getString("AssistantAttributeTypeName"));
				model.setAttributeBasicType(rs.getInt("AttributeBasicType"));
			}
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			DBUtil.setNullObject(con, pStmt, rs);
		}
		return model;
	}

	/**
	 * 修改方法
	 */
	@Override
	public void updateAttributeType(PBaseMaterialAssistantattributetype model)
	throws Throwable {
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;

		try {
			con = dataSource.getConnection();
			pStmt = con
					.prepareStatement("update P_base_material_assistantattributetype "
							+ "set AssistantAttributeTypeCode = ?,AssistantAttributeTypeName = ?,AttributeBasicType = ? "
							+ "where AssistantAttributeTypeID = ?");
			pStmt.setString(1, model.getAssistantAttributeTypeCode());
			pStmt.setString(2, model.getAssistantAttributeTypeName());
			pStmt.setInt(3, model.getAttributeBasicType());
			pStmt.setInt(4, model.getAssistantAttributeTypeId());
			pStmt.executeUpdate();

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			DBUtil.setNullObject(con, pStmt, rs);
		}
	}

	@Override
	public List<PBaseMaterialAssistantattributetype> getAllAttributeType(
			int basicType) throws Throwable {
		List<PBaseMaterialAssistantattributetype> list = new ArrayList<PBaseMaterialAssistantattributetype>();
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;

		try {
			con = dataSource.getConnection();
			pStmt = con
					.prepareStatement("select * from P_base_material_assistantattributetype where AttributeBasicType = ?");
			pStmt.setInt(1, basicType);
			rs = pStmt.executeQuery();

			while (rs.next()) {
				PBaseMaterialAssistantattributetype pma = new PBaseMaterialAssistantattributetype();
				pma.setAssistantAttributeTypeId(rs
						.getInt("AssistantAttributeTypeID"));
				pma.setAssistantAttributeTypeCode(rs
						.getString("AssistantAttributeTypeCode"));
				pma.setAssistantAttributeTypeName(rs
						.getString("AssistantAttributeTypeName"));
				pma.setAttributeBasicType(rs.getInt("AttributeBasicType"));

				list.add(pma);
			}
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			DBUtil.setNullObject(con, pStmt, rs);
		}
		return list;
	}

	@Override
	public List<PBaseMaterialAssistantattributetype> getByCode(
			PBaseMaterialAssistantattributetype model) throws Throwable {
		List<PBaseMaterialAssistantattributetype> list = new ArrayList<PBaseMaterialAssistantattributetype>();
		Connection con = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			pStmt = con
					.prepareStatement("select * from P_base_material_assistantattributetype where AssistantAttributeTypeCode = ?");
			pStmt.setString(1, model.getAssistantAttributeTypeCode());
			rs = pStmt.executeQuery();

			while (rs.next()) {
				PBaseMaterialAssistantattributetype typeModel = new PBaseMaterialAssistantattributetype();
				typeModel.setAssistantAttributeTypeId(rs
						.getInt("AssistantAttributeTypeID"));
				typeModel.setAssistantAttributeTypeCode(rs
						.getString("AssistantAttributeTypeCode"));
				typeModel.setAssistantAttributeTypeName(rs
						.getString("AssistantAttributeTypeName"));
				typeModel.setAttributeBasicType(rs.getInt("AttributeBasicType"));
				
				list.add(typeModel);
			}
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			DBUtil.setNullObject(con, pStmt, rs);
		}
		return list;
	}

}
